﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.Portal.Mvc.Models;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    public class MasterDataController : SharpController
    {
        // GET: MasterDataDTO

        public ActionResult Index()
        { 
            var model = new MasterDataIndexModel();
             
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                model.LeftTree = proxy.Channel.GetMasterDataSort();
            }
            
            return View(model);
        }

        private void Build(IEnumerable<MasterDataCategoryDTO> table, zTreeNode node)
        {
            foreach (var row in table)
            {
                var _node = new zTreeNode
                {
                    Name = row.Name,
                    Id = row.Code,
                    Raw = row
                };

                node.Children.Add(_node);
                node.IsParent = node.Children.Any();
                node.Open = true;
            }
        }


        [Permission("新增", 1, "002003001"), AutomaticTemplate]
        public ActionResult Create(string category)
        {
            if (string.IsNullOrEmpty(category))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var model = new MasterDataModel();
            model.Category = category;

            return View(model);
        }

        [Permission("编辑", 2,  "002003002"), AutomaticTemplate]
        public ActionResult Edit(Guid id, string category)
        {
            if (string.IsNullOrEmpty(category))
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            MasterDataModel vm;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataInfo(id).To<MasterDataModel>();
                vm.DropDown = proxy.Channel.GetMasterData();
            }

            return View(vm);
        }

        
        public ActionResult Save(MasterDataModel model)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.SaveMasterData(model.To<MasterDataDTO>());

                return this.JsonNet(res);
            }
        }

        #region 分页数据

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var pageInfo = proxy.Channel.MasterDataGetPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }
        public ActionResult GetCategoryPaged(DataTableParameter parameters)
        {
            PagedResult<MasterDataCategoryDTO> pageInfo = new PagedResult<MasterDataCategoryDTO>();
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                pageInfo.Data = proxy.Channel.GetMasterDataCategory().ToList();
                pageInfo.TotalRecords = pageInfo.Data.Count;
                return this.Store(pageInfo);
            }
        }
        #endregion


        [Permission("删除", 3,  "002003003")]
        public ActionResult Delete(Guid[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterData(ids);

                return this.JsonNet(res);
            }

        }

        public ActionResult GetMasterDataSort()
        {
            IList<MasterDataCategoryDTO> list;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                list = proxy.Channel.GetMasterDataSort();
            }
            return this.JsonNet(list);
        }
        public ActionResult GetMasterData()
        {
            IList<MasterDataSimpleDTO> list;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                list = proxy.Channel.GetMasterData();
            }
            return this.JsonNet(list);
        }

        #region 分类
        [AutomaticTemplate]
        public ActionResult CategoryCreate()
        {
            var vm = new MasterDataCategoryViewModel();
            vm.UI.PostAction = "CategorySave";
            return View(vm);
        }
        [AutomaticTemplate]
        public ActionResult CategoryEdit(int id)
        {
            MasterDataCategoryViewModel vm;
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                vm = proxy.Channel.GetMasterDataCategoryInfo(id).To<MasterDataCategoryViewModel>();
            }
            vm.UI.PostAction = "CategorySave";
            vm.UI.FieldsHandle = (formSections) =>
            {
                formSections.Hide(nameof(MasterDataCategoryViewModel.Code) );
            };
            return View(vm);
        }
        [System.Web.Mvc.HttpPost]
        public ActionResult CategoryDelete(int[] ids)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.DeleteMasterDataCategory(ids);

                return this.JsonNet(res);
            }
        }
        [System.Web.Mvc.HttpPost]
        public ActionResult CategorySave(MasterDataCategoryViewModel model)
        {
            using (var proxy = new WebApiClientServiceProxy<IPlatformService>())
            {
                var res = proxy.Channel.SaveMasterDataCategory(model.To<MasterDataCategoryDTO>());

                return this.JsonNet(res);
            }
        }
        #endregion
    }
}